/*
	----------------------------------------------------------
	文件上传示例
	----------------------------------------------------------
*/

$(function () {
	var upload = function(){
		
		var up = new Object();
		
		up.options = {
			config : {
				url : undefined,
				node : undefined,
				size : 1024 * 5
			},
			
			form : undefined

		};
		
		
		//事件
		up.event = {
			onOpenFileEnd : function(params){
				return false;
			}
		};
		
		//初始化
		up.init = function(o){
			
			up.options.config.url = o.url? o.url : "";
			up.options.config.node = o.node;
			up.options.config.size = o.size || up.options.config.size;
			
			up.createForm();
			
		}
		
		//创建form表单
		up.createForm = function(){
			var form = $("<form></form>");
			form.attr("enctype","multipart/form-data");
			form.attr("id","xyd-form-upload-file").attr("action","");
			
			form.attr("method","post");
			form.attr("onsubmit","return false;");
			form.css({"opacity": "1","filter": "alpha(opacity=100)","position":"absolute","index":"-1"});
			$(up.options.config.node).append(form);
			up.options.form = form;
			
			up.insertInput();
			
		}
		
		//取文件信息
		up.getFileInfo = function(params){
			var data = {};		
			try{
				data = {
					name : up.getFileName(params.files[0].name),
					size : params.files[0].size,
					type : up.getFileType(params.files[0].name)
				};
				
			}catch(e){
				try{
					data = {
						name : up.getFileName(params.value),
						size : undefined,
						type : up.getFileType(params.value)
					};
					var fso = new ActiveXObject("Scripting.FileSystemObject");
					data.size = fso.GetFile(params.value).size;
				}catch(e){
					alert('检测到您可能使用的IE浏览器，请调低安全级别设置为 "低"，否则无法正常使用');
				}
			}
			
			return data;
			
		}
		
		//取文件名
		up.getFileName = function(o){
			return o.substring(o.lastIndexOf("\\")+1);
		}
		
		//取后缀名
		up.getFileType = function(val){
			try {
				var str =  /\.[^\.]+$/.exec(val)+"";
				var b = str.replace(/\./g,"");
				return b;
			} catch (e) {}
			return null;
		};
		
		//格式化文件大小
		up.formatSize = function( size, pointLength) {
			var unit = ['B', 'KB', 'MB', 'GB', 'TB' ];
			var i = 0;
			while (size >= 1024 ) {
				i += 1;
				size = size / 1024;
			}
			if(String(size).indexOf(".") + 1 < 1){
				pointLength = 0;
			}	
			return size.toFixed(pointLength || 0) + " " + unit[i];
		}
		
		
		
		//打开文件
		up.startOpenFile = function(){
			var io = up.getLastInput();
			var len = (typeof io.files == 'undefined') ? io.value.length : io.files.length;		
			if(len > 0){
				io = up.insertInput();
			}
			
			return up.callInput(io);
		}
		
		//插入一个表单
		up.insertInput = function(){
			if(up.isInputNull()){
				return;
			}
			var inputElement = document.createElement("input");
				$(inputElement).attr("type","file").attr("name","form-io").attr("multiple","multiple");
				inputElement.onchange = function(){
					return up.event.onOpenFileEnd(this);
				}
				
			up.options.form.append($(inputElement));
			
			return inputElement;
		}
		
		//取最后一个表单对象
		up.getLastInput = function(){
			var io = up.options.form.find("input[type='file']");
			return io[io.length-1];
		}
		
		//删除表单
		up.deleteInput = function(index){
			up.options.form.find("input[type='file']")[index].remove();
		}
		
		//判断最后一个表单是否为空
		up.isInputNull = function(){
			var io = up.getLastInput();
			if(typeof io == 'undefined'){
				return;
			}
			var len = (typeof io.files == 'undefined') ? io.value.length : io.files.length;	
			if(len < 1){
				return true;
			}
			return false;
		}
		
		//调用input click
		up.callInput = function(o){
			return o.click();
		}
		
		
		//返回自身
		return up;
	}
	
	
	var upload_file = new upload();
	$("#btn-fileUplaod").click(function(){	
		$.get('fileUpload-ajax.html', {}, function(str){
			layer.open({
				type: 1,
				area: ['500px', '300px'],
				title: '文件上传',
				content: str, //注意，如果str是object，那么需要字符拼接。
				btn: ['确定', '取消'],
				yes:function(i){
					layer.confirm('您确认保存这条数据吗?', {title:'提示'}, function(index){
						layer.close(i);
						layer.msg('数据保存成功',{time:2000},function(){});
					});
				}
			});
			
			upload_file.event.onOpenFileEnd = function(t){
				upload_file.getFileInfo(t);
			}
			$("#openFile").click(function(){
				upload_file.startOpenFile();
				
			});
			
			upload_file.init({
				url:"",
				node : "#upload-file-box"
			});
			
		});
	});
	$("#btn-fileUplaod").click();
	
	
});